/*
 * @Author: your name
 * @Date: 2021-07-07 15:01:29
 * @LastEditTime: 2021-08-24 16:10:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \exc-tl1-2\SRC\uart.c
 */
#include "uart.h"
uartStruct m_uart0 __attribute__((at(0x10000300))), m_uart1 __attribute__((at(0x10000320)));
//uartStruct m_uart0;
//uartStruct m_uart1;
uartStruct m_uart2 __attribute__((at(0x10000d10)));


u8 logEN = 1, testEN = 0;
void uart_init(u8 Cfg)
{
	u32 ClkDiv;
	ClkDiv = fnDl645MainClock_Get();
	switch (Cfg & 0xE0)
	{
	case USART_BPS_4800: //4800
		ClkDiv = ClkDiv / (4800 * 16) - 1;
		break;
	case USART_BPS_9600: //9600
		ClkDiv = ClkDiv / (9600 * 16) - 1;
		break;
	case USART_BPS_115200: //115200
		ClkDiv = ClkDiv / (115200 * 16) - 1;
		break;
	default:
		ClkDiv = ClkDiv / (115200 * 16) - 1; //115200
		break;
	}
	UART0->BAUD = ClkDiv;
	UART0->CTRL = (1 << 0) | (3 << 5) | (7 << 1);
	UART0->STA = 0x3f;
	
	UART1->BAUD = ClkDiv;
	UART1->CTRL = (1 << 0) | (3 << 5) | (7 << 1);
	UART1->STA = 0x3f;
	
	UART2->BAUD = fnDl645MainClock_Get() / (4800 * 16) - 1;
	UART2->CTRL = (1 << 0) | (3 << 5) | (7 << 1);
	UART2->STA = 0x3f;
	
	NVIC_SetPriority(UART0_IRQn, 0);
	NVIC_SetPriority(UART1_IRQn, 0);
	NVIC_SetPriority(UART2_IRQn, 0);
	
	NVIC_EnableIRQ(UART0_IRQn);
	NVIC_EnableIRQ(UART1_IRQn);
	NVIC_EnableIRQ(UART2_IRQn);
	
	memset(&m_uart0, 0, sizeof(m_uart0));
	memset(&m_uart1, 0, sizeof(m_uart1));
	return;
}
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
void uart_switch_baud(u8 Cfg)
{
	u32 ClkDiv;
	ClkDiv = fnDl645MainClock_Get();
	switch (Cfg & 0xE0)
	{
	case USART_BPS_4800: //4800
		ClkDiv = ClkDiv / (4800 * 16) - 1;
		break;
	case USART_BPS_9600: //9600
		ClkDiv = ClkDiv / (9600 * 16) - 1;
		break;
	case USART_BPS_115200: //115200
		ClkDiv = ClkDiv / (115200 * 16) - 1;
		break;
	default:
		ClkDiv = ClkDiv / (115200 * 16) - 1; //115200
		break;
	}
	//UART0->BAUD = ClkDiv;
	UART1->BAUD = ClkDiv;
}
#endif
void uart0_send(u8 *tx_buf, u16 len)
{
	if (len < 1)
		return;
	m_uart0.tx_len = len;
	m_uart0.tx_data = tx_buf;
	m_uart0.tx_len--;
	UART0->TXD = *(m_uart0.tx_data++);
	m_uart0.send_cplt = 1;
}
void uart1_send(u8 *tx_buf, u16 len)
{
	if (len < 1)
		return;
	m_uart1.tx_len = len;
	m_uart1.tx_data = tx_buf;
	m_uart1.tx_len--;
	UART1->TXD = *(m_uart1.tx_data++);
	m_uart1.send_cplt = 1;
}

void uart2_send(u8 *tx_buf, u16 len)
{
	if (len < 1)
		return;
	m_uart2.tx_len = len;
	m_uart2.tx_data = tx_buf;
	m_uart2.tx_len--;
	UART2->TXD = *(m_uart2.tx_data++);
	m_uart2.send_cplt = 1;
}

void uart0_rx_dataReset(void)
{
	m_uart0.rx_len = 0;
	m_uart0.timeOut = 0;
	m_uart0.rx_ok = 0;
	memset(pcUart.pc_rx_buf, 0, sizeof(pcUart.pc_rx_buf));
}
void uart1_rx_dataReset(void)
{
	m_uart1.rx_len = 0;
	m_uart1.timeOut = 0;
	m_uart1.rx_ok = 0;
	memset(mainUart.my_at_readbuf, 0, sizeof(mainUart.my_at_readbuf));
}

void uart2_rx_dataReset(void)
{
	m_uart2.rx_len = 0;
	m_uart2.timeOut = 0;
	m_uart2.rx_ok = 0;
	memset(pcUart.bl0939_rx_buf, 0, sizeof(pcUart.bl0939_rx_buf));
//	memset(mainUart.my_at_readbuf, 0, sizeof(mainUart.my_at_readbuf));
}
